/*
 * Copyright 2012 The Netty Project
 *
 * The Netty Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package io.netty.example.echo;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.io.UnsupportedEncodingException;

/**
 * Handler implementation for the echo client.
 * echo 客户端实现类
 * It initiates the ping-pong traffic between the echo client and server by sending the first message to the server.
 * 数据会像乒乓球一样 , 在客户端和服务端来回传送
 * <p>
 * ChannelInboundHandlerAdapter , Inbound 入线数据流
 */
public class EchoClientHandler extends ChannelInboundHandlerAdapter {

    private ByteBuf firstMessage;
    int i = 1;

    /**
     * Creates a client-side handler.
     * 创建客户端处理器,构造函数
     */
    public EchoClientHandler() {
//        char[] his = new char[]{"H","I"};
        //初始化消息大小
        firstMessage = Unpooled.buffer(EchoClient.SIZE);
//        for (int i = 0; i < 3; i++) {
//            //初始化消息数据,循环将数字添加到ByteBuf中
//            firstMessage.writeByte((byte) i);
//        }
    }

    /**
     * 当完成与服务器的链接时,触发此动作
     * @param ctx
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) {

        //当客户端激活时
        //将固定数据,写入到channel中,写给服务端
        firstMessage.writeByte(i);
        System.out.println("channelActive : writeAndFlush : HI");
        ctx.writeAndFlush(firstMessage);
        System.out.println("channelActive end : writeAndFlush : HI");
    }

    /**
     * 读取服务器数据时,触发此动作
     * @param ctx
     * @param msg
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        //当读取到数据时,将消息重写回channelpipline
        System.out.println("channelRead : write : " + msg.toString());
        firstMessage = Unpooled.buffer(EchoClient.SIZE);
        firstMessage.writeByte(++i);
        ctx.write(firstMessage);
        System.out.println("channelRead end : write : " + msg.toString());
    }

    /**
     * 一批次数据读取完成后, 触发此动作
     * @param ctx
     */
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) {
        //当数据读取完毕后,刷新chnnel,更新缓存区
        System.out.println("channelReadComplete : flush ");
        ctx.flush();
        System.out.println("channelReadComplete end : flush ");
    }

    /**
     * 流程中的任何异常
     * 都会触发此动作
     * @param ctx
     * @param cause
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        // Close the connection when an exception is raised.
        //打印出异常栈
        cause.printStackTrace(System.out);
        // 当有异常抛出时,关闭链接
        ctx.close();
    }
}
